package com.example.singleactivitytemplate.ui.fragment.test

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.fragment.NavHostFragment
import com.example.singleactivitytemplate.R
import com.example.singleactivitytemplate.databinding.TestFragmentBinding
import com.example.singleactivitytemplate.ui.base.BaseFragment
import com.example.singleactivitytemplate.ui.fragment.setting.SettingViewModel


class TestFragment : BaseFragment<TestFragmentBinding, SettingViewModel>(),
    View.OnClickListener,View.OnLongClickListener {
        private var navHostController: NavController? = null
        override fun getViewBindingClass(
            inflater: LayoutInflater, container: ViewGroup?
        ): TestFragmentBinding {
            return TestFragmentBinding.inflate(inflater, container, false)
        }

        override fun getViewModelClass(): Class<SettingViewModel> {
            return SettingViewModel::class.java
        }

        override fun initData() {
            //设置主界面
            val navHostFragment =
                childFragmentManager.findFragmentById(R.id.setting_navigation) as NavHostFragment?
            navHostController = navHostFragment?.navController
        }

        override fun initOnListener() {

        }

        override fun initObserver() {
            mViewModel?.mFragmentId?.observe(this) {
                switchFragment(it)
            }

        }

        /**
         * 切换Fragment
         * @param fragmentId 目标Fragment的Id
         */
        private fun switchFragment(fragmentId: Int) {
            if (navHostController?.popBackStack(fragmentId, false) == false) {
                val builder = NavOptions.Builder().apply {}
                builder.setLaunchSingleTop(true)
                navHostController?.navigate(fragmentId, null, builder.build())
            }
        }


        override fun onClick(v: View?) {

        }
        override fun onLongClick(v: View?): Boolean {

            return true
        }
        override fun detach() {
        }


    }